package com.itheima.health.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;

/**
 * @author ：李帅
 * @date ：Created in 2022年06月07日
 * @description ：
 * @version: 1.0
 */

/**
 * 短信发送工具类
 */
public class SMSUtils {

    public static void main(String[] args) {
        // 用户点击了 获取验证码
        System.out.println("用户点击获取验证码");
        // 1:获取手机号
        String phoneNum = "13283707112";
        //2: 产生验证码并发送
        //调用工具类生成验证码
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        // code 就是验证码
        System.out.println("产生验证码是:"+code);
        String signName = "传智健康项目基手机号";
        String templateCode = "SMS_175532136";
        // 通过阿里云给 指定的手机 发生指定签名+模板的验证码
        sendMessage(signName,templateCode,phoneNum,code+"");
        System.out.println("短信已经发完了");



    }
    /**
     * 发送短信
     * @param signName 签名
     * @param templateCode 模板
     * @param phoneNumbers 手机号
     * @param param 参数
     */
    public static void sendMessage(String signName,
                                   String templateCode,String phoneNumbers,String param){
        DefaultProfile profile = DefaultProfile
                .getProfile("cn-hangzhou",
                        "",
                        "");
        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSysRegionId("cn-hangzhou");
        request.setPhoneNumbers(phoneNumbers);
        request.setSignName(signName);
        request.setTemplateCode(templateCode);
        request.setTemplateParam("{\"code\":\""+param+"\"}");
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(response.getMessage());
            if(response.getMessage().contains("OK")){
                System.out.println("短信发送成功");
            }
        }catch (ClientException e) {
            e.printStackTrace();
        }
    }

}
